package com.ruoyi.web.controller.system;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.PwsTable;
import com.ruoyi.system.service.PwsTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * pws 前端控制器
 * </p>
 *
 * @author tianfu-li
 * @since 2025-44-27
 */
@RestController
@RequestMapping("/pwsTable")
@Api(tags = "pws")
public class PwsTableController {
    @Autowired
    private PwsTableService pwsTableService;
    @GetMapping("/getPwsTableByOrderNoOrOrderStartDate")
    @ApiOperation(value = "查询订单信息通过订单号或订单开始日期")
    public R<List<PwsTable>> getPwsTableByOrderNoOrOrderStartDate(
            @RequestParam(value = "orderNo", required = false, defaultValue = "") String orderNo,
            @RequestParam(value = "orderStartDate", required = false, defaultValue = "")
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDateTime orderStartDate){
        List<PwsTable> pwsTables=pwsTableService.getPwsTableByOrderNoOrOrderStartDate(orderNo,orderStartDate);
        if(pwsTables.size()>0){
            return R.ok(pwsTables);
        }else{
            return R.fail("查询订单信息失败");
        }
    }
    @GetMapping("/getPwoBySap")
    @ApiOperation(value = "根据日期获取当日 pwo 订单")
    public R getPwoBySap(
            @RequestParam(value = "date")
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            Date date,
            @RequestParam(value = "userId") String userId
            ) throws JsonProcessingException {
        boolean flag=pwsTableService.getPwoBySap(date,userId);
        if(flag){
            return R.ok();
        }else{
            return R.fail("根据日期获取当日 pwo 订单失败");
        }
    }
    @GetMapping("/getPwsTableFromDatetime")
    @ApiOperation("根据日期与类型查询订单")
    public AjaxResult getPwsTableFromDatetime(

            @RequestParam(value = "order_no", required = false, defaultValue = "") String orderNo,
            @RequestParam(value = "Order_Start_Date", required = false, defaultValue = "")
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate orderStartDate,
            @RequestParam(value = "order_type", required = false, defaultValue = "") String orderType)
    {
       return AjaxResult.success(pwsTableService.selectByPrimaryKeyFromOrderNo(orderNo,orderStartDate,orderType)) ;
    }

}
